<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="sealte.hvpms.pojo.*,sealte.hvpms.dao.impl.*,sealte.hvpms.util.HVPMSTools,java.util.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>仓鼠网上投票系统HVPMS</title>
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<link href="css/vp_view.css" rel="stylesheet" type="text/css" />
<script src="js/jquery-1.4.4.min.js" type="text/javascript"></script>
<script src="js/vp_view_frame.js" type="text/javascript"></script>
</head>
<body>
<%
	String fid_str = request.getParameter("fid");
	String voter_ipaddr = request.getRemoteAddr();
	if(fid_str!=null&&!fid_str.equals("")&&HVPMSTools.isNum(fid_str)){
		int fid = Integer.parseInt(fid_str);
		PollDAOImpl pb = new PollDAOImpl();
		Poll poll = pb.getPoll(fid);
		if(poll.getIsexpired().equals("N")){
			if(poll.getIsenabled().equals("Y")){
				PollOptionDAOImpl pob = new PollOptionDAOImpl();
				List<PollOption> poAll = pob.getPOAll(fid);
				Iterator<PollOption> iter = poAll.iterator();
%>
<script type="text/javascript">
var ruleNum_str = <%=poll.getRule_int()%>;
var ruleNum = parseInt(ruleNum_str);
$(document).ready(function(){
	$('#frameTopArea').hide();
	$('#rule2').hide();
	$('#rule3').hide();
	$('#rule4').hide();
	$('#rule5').hide();
	if(ruleNum==2){
		$('#rule2').show();
	}else if(ruleNum==3){
		$('#rule3').show();
	}else if(ruleNum==4){
		$('#rule4').show();
	}else if(ruleNum==5){
		$('#rule5').show();
	}
});
function validateToSubmit(){
	var reg = /^\s+|\s+$/g;
	var i = 0;
	var max = <%=poll.getMaxanswers() %>;
	var chooseFlag = false;
	var flag = false;
	$(':checkbox').each(function(){
		if($(this).attr("checked")){
			i++;
		}
	});
	if(i>0){
		if(i<=max){
			chooseFlag = true;
		}else{
			alert("您最多只能选"+max+"项");
		}
	}else{
		alert("您还没选择投票项");
	}
	if(ruleNum==1){
		flag = true;
	}else if(ruleNum==2){
		var validateCode = $('#validate_code').val();
		validateCode = validateCode.replace(reg,"");
		if(validateCode!=""){
			flag = true;
		}else{
			alert("验证码不能为空");
		}
	}else if(ruleNum==3){
		var voterEmail = $('#voterEmail').val();
		voterEmail = voterEmail.replace(reg,"");
		if(voterEmail!=""){
			var reg_email = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
			if(reg_email.test(voterEmail)){
				flag = true;
			}else{
				alert("邮箱格式不正确");
			}
		}else{
			alert("邮箱不能为空");
		}
	}else if(ruleNum==5){
		var voterIdcard = $('#voterIdcard').val();
		voterIdcard = voterIdcard.replace(reg,"");
		if(voterIdcard!=""){
			flag = true;
		}else{
			alert("身份证号不能为空");
		}
	}else if(ruleNum==4){
		flag = true;
	}
	if(chooseFlag&&flag){
		$('#voteForm').submit();
	}
}
</script>

<div id="topArea">
	<table id="topArea_table">
		<tr>
			<td align="right" width="10%">标题：</td>
			<td align="left" width="30%"><%=poll.getName() %></td>
			<td align="right" width="15%">最多可选项数：</td>
			<td align="left"><%=poll.getMaxanswers() %></td>
		</tr>
		<tr>
			<td align="right" valign="top">描述：</td>
			<td colspan="3" align="left">
			<%=poll.getDescriptions()%>
			</td>
		</tr>
	</table>
</div>
<span id="show_vp_res">
<%
	String message = (String)request.getAttribute("message");
	if(message!=null&&!message.equals("")){
		out.println("["+message+"]");
	}
%>
</span>
<div id="middleArea">
<form action="VPVote" method="POST" id="voteForm">
<input type="hidden" name="fid" value="<%=fid %>"/>
<table id="middleArea_table">
	<tr><th width="2%">序号</th><th width="33%">选项</th><th width="32%">相关图片</th><th width="33%">相关链接</th></tr>
<%
	while(iter.hasNext()){
		PollOption po = iter.next();
%>
	<tr>
		<td width="10%" valign="top"><input type="checkbox" name="poll_options" value="<%=po.getId()%>" /><%=po.getId()%></td>
		<td valign="top"><%=po.getContent()%></td>
		<td valign="top">
		<%
			if(!po.getImage().equals("")){
		%>
		<img src="<%=po.getImage()%>" onload="proDownImage(this);" />
		<%} %>
		</td>
		<td valign="top"><%=po.getLink()%></td>
	</tr>
<%		
	}	
%>
<tr id="submitButton">
	<td align="left" colspan="4">
		<span id="rule2">
			验证码：<input type="text" size="6" name="validateCode" id="validate_code">
			<img src="util/identifycode.jsp" id="checkimg"/>
			<a href="javascript:changeVCode()"><font size="2">换一张</font></a>
		</span>
		<span id="rule3">
			您的邮箱：<input type="text" name="voter_email" id="voterEmail">
		</span>
		<span id="rule4">
			[每个IP限投一次]
			<input type="hidden" name="voter_ipaddr" value="<%=voter_ipaddr %>">
		</span>
		<span id="rule5">
			您的身份证号：<input type="text" name="voter_idcard" id="voterIdcard">
		</span>
		<input type="button" value="提交" onclick="validateToSubmit()">
	</td>
</tr>
</table>
</form>
</div>
<%			}else if(poll.getIsenabled().equals("N")){
				out.println("该投票被创建者设置为【不可投】");
			}
		}else if(poll.getIsexpired().equals("Y")){
			out.println("该投票已到期，不可投");
		}
	}else{
		out.println("出错了。。。");
	}
%>
</body>
</html>